package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    /*
    * 新增分组
    * */
    @Override
    public void addGroup(CheckGroup checkGroup,List<String> checkitemIds) {
//        List<CheckItem> checkItems = checkGroup.getCheckItems();
//        Integer id = checkGroup.getId();
//        for (CheckItem checkItem : checkItems) {
//            checkGroupDao.addGroupItm(id,checkItem.getId());
//        }
        //添加分组信息
        checkGroupDao.addGroup(checkGroup);

        Integer id = checkGroup.getId();
        for (String checkitemId : checkitemIds) {
            checkGroupDao.addGroupItm(id,checkitemId);

        }



    }
    /*
    * 分页查询
    * */
    @Override
    public PageResult list(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //分页拆查询数据库
       Page<CheckGroup> page= checkGroupDao.sselectBYGoup(queryPageBean.getQueryString());
       return new PageResult(page.getTotal(),page.getResult());
    }
/*
* 根据id查询
* */
    @Override
    public CheckGroup findByid(Integer id) {
       CheckGroup checkGroup = checkGroupDao.findByid(id);
        return checkGroup;
    }

    @Override
    public List<Integer> findcheckid(Integer id) {
      List<Integer> ids = checkGroupDao.selcheckgoupitm(id);
        return ids;
    }
/*
* 根据id修改
* */
    @Override
    public void updateBid(CheckGroup checkGroup,List<String> checkitemIds) {
        //先修改基本信息
        checkGroupDao.update(checkGroup);
        //然后删除之前的项目 重新添加
        Integer id = checkGroup.getId();
        checkGroupDao.delectcheckitem(id);
        //重新添加项目
        for (String checkitemId : checkitemIds) {
            checkGroupDao.addGroupItm(id,checkitemId);
        }
    }
/*
* 根据id删除
* */
    @Override
    @Transactional
    public void deteleBid(Integer id) {
        //在根据基本表id 删除项目表
        checkGroupDao.deletechekgroupitm(id);

        //根据id删除基本信息
        checkGroupDao.delsteBid(id);

    }

    /*
* 查询检查组信息
* */
    @Override
    public List<CheckGroup> selectGoup() {
      List<CheckGroup> checkGroups =  checkGroupDao.selectGoup();
        return checkGroups;
    }

}
